<template>
    <div class="list">
        <div class="table" v-if="active==1">
            <el-form :model="params" style="width: 100%;">
                <el-container>
                    <!--内容-->
                    <el-form-item>
                        <el-input v-model="params.content" style="width: 200px;"></el-input>
                    </el-form-item>
                    <!--字段类型-->
                    <el-form-item style="margin-left: 10px">
                        <el-select v-model="params.field" :value="params.field" style="width: 200px;">
                            <el-option
                                    v-for="type in typeList"
                                    :label="type.value"
                                    :value="type.key"
                                    :key="type.key"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <!--开庭状态-->
                    <el-form-item style="margin-left: 10px">
                        <el-select v-model="params.status" :value="params.status" style="width: 130px;">
                            <el-option value="" label="选择开庭状态"></el-option>
                            <el-option value="1" label="未开庭"></el-option>
                            <el-option value="2" label="已开庭"></el-option>
                        </el-select>
                    </el-form-item>
                    <!--自定义排序-->
                    <el-form-item style="margin-left: 10px">
                        <el-select v-model="sortModel" :value="sortModel" @change="handleSort">
                            <el-option label="自定义排序" value=""></el-option>
                            <el-option
                                    v-for="sort in sortList"
                                    :label="sort.value"
                                    :value="sort.value"
                                    :key="sort.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item style="margin-left: 10px">
                        <el-button type="primary" @click="handleSearch">检索</el-button>
                        <el-button @click="handleAdd">添加</el-button>
                    </el-form-item>
                </el-container>
            </el-form>

            <el-table :border="true" :data="pageResult.records">
                <el-table-column prop="id" v-if="false"></el-table-column>
                <el-table-column prop="caseid" v-if="false"></el-table-column>
                <el-table-column label="案号" prop="caseNo" width="150px"></el-table-column>
                <el-table-column label="委托人" prop="caseWtr"></el-table-column>
                <el-table-column label="审判程序" prop="flow"></el-table-column>
                <el-table-column label="立案时间" prop="lnsj"></el-table-column>
                <el-table-column label="开庭时间" prop="ktsj"></el-table-column>
                <el-table-column label="审判机关" prop="spjg"></el-table-column>
                <el-table-column label="法庭" prop="ft"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-dropdown trigger="click" @command="handleCommandClick">
                            <el-button>
                                操作<i class="el-icon-arrow-down el-icon--right"></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item :command="beforeHandlerCommand(scope.row,'show')">查看
                                </el-dropdown-item>
                                <el-dropdown-item v-if="scope.row.status!='2'"
                                                  :command="beforeHandlerCommand(scope.row,'update')">修改
                                </el-dropdown-item>
                                <el-dropdown-item v-if="scope.row.status!='2'"
                                                  :command="beforeHandlerCommand(scope.row,'delete')">删除
                                </el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>

            <el-footer class="case-list-footer">
                <el-pagination
                        style="margin: 10px auto"
                        background
                        layout="prev, pager, next"
                        :total="pageResult.total"
                        :page-size="pageResult.size"
                        class="pagination"
                        :current-page="pageResult.current"
                        @current-change="handleIndexChange"
                >
                </el-pagination>
            </el-footer>
        </div>

        <el-dialog
                title="详细信息"
                :visible.sync="centerDialogVisible"
                width="70%"
                center>
            <fy-details :fy="fy"></fy-details>
        </el-dialog>

        <div style="width: 100%;" v-if="active==2">
            <fy-edit
                    :fy="fy"
                    :status="status"
                    @back="handleBack"
                    @update="handleUpdateSubmit"
                    @add="handleAddSubmit">
            </fy-edit>
        </div>
    </div>
</template>

<script>
    //引入组件
    const FyDetails = () => import('./FyDetails.vue')
    const FyEdit = () => import('./FyEdit.vue')
    //引入服务类
    import {FyService} from "../../../model/gs/FyService";

    const fyService = FyService.getInstance()

    export default {
        data() {
            return {
                centerDialogVisible:false,
                status: 'add',
                active: 1,
                params: {
                    status: '',
                    field: 'case_no'
                },
                pageResult: {},
                //搜索类型
                typeList: [
                    {
                        key: 'case_no',
                        value: '案号搜索'
                    },
                    {
                        key: 'ft',
                        value: '法庭搜索'
                    },
                    {
                        key: 'fg',
                        value: '法官搜索'
                    }
                ],
                //排序
                sortModel: '',
                sortList: [
                    {
                        key: 'ktsj',
                        value: '开庭时间从高到低',
                        type: 'desc'
                    },
                    {
                        key: 'ktsj',
                        value: '开庭时间从低到高',
                        type: 'asc'
                    },
                    {
                        key: 'lnsj',
                        value: '收案时间从高到低',
                        type: 'desc'
                    },
                    {
                        key: 'lnsj',
                        value: '收案时间从低到高',
                        type: 'asc'
                    },
                ],
                fy: {}
            }
        },
        methods: {
            getFyList() {
                fyService.getByPage(this.params).then(response => {
                    if (response.data.code === 0) {
                        this.pageResult = response.data.data
                    }
                })
            },
            //排序的方法
            handleSort() {
                if (this.sortModel == '') {
                    this.params.sortRule = ''
                    this.params.sortColumn = ''
                    return
                }
                this.sortList.forEach(item => {
                    if (item.value == this.sortModel) {
                        this.params.sortRule = item.type
                        this.params.sortColumn = item.key
                    }
                })
            },
            handleSearch() {
                this.params.page = 1
                this.getFyList()
            },
            //处理角标变化的方法
            handleCommandClick(command) {
                switch (command.type) {
                    case 'show':
                        this.handleShowDetails(command.fy)
                        break
                    case 'update':
                        this.handleUpdate(command.fy)
                        break
                    case 'delete':
                        this.handleDelete(command.fy)
                        break
                }
            },
            handleDelete(fy){
                fyService.removeById(fy.id).then(response => {
                    if(response.data.code === 0){
                        this.$message.success({
                            message:'删除成功',
                            duration:1000
                        })
                        //刷新数据
                        this.getFyList()
                    }
                })
            },
            //查看详情的方法
            handleShowDetails(fy) {
                this.fy = fy
                this.centerDialogVisible = true
            },
            //角标发生变化之前的操作
            beforeHandlerCommand(fy, type) {
                return {
                    fy, type
                }
            },
            handleIndexChange(index) {
                this.params.page = index
                this.getFyList()
            },
            handleUpdate(fy) {
                this.status = 'update'
                fyService.getById(fy.id).then(response => {
                    if(response.data.code === 0){
                        this.fy = response.data.data
                        this.active = 2
                    }
                })
            },
            handleUpdateSubmit() {
                //校验
                if (!fyService.check(this.fy)){
                    return
                }
                //发送请求
                fyService.update(this.fy).then(response => {
                    if(response.data.code === 0){
                        this.$message.success({
                            message:'修改成功',
                            duration:1000
                        })
                        //刷新数据
                        this.handleSearch()
                        //重置fy对象
                        this.handleBack()
                    }
                })
            },
            handleAdd() {
                this.status = 'add'
                this.active = 2
            },
            handleAddSubmit() {
                //校验
                if (!fyService.check(this.fy)){
                    return
                }
                //发送请求
                fyService.save(this.fy).then(response => {
                    if(response.data.code === 0){
                        this.$message.success({
                            message:'添加成功',
                            duration:1000
                        })
                        //刷新数据
                        this.handleSearch()
                        //重置fy对象
                        this.handleBack()
                    }
                })
            },
            handleBack() {
                this.fy = {}
                this.active = 1
            }
        },
        created() {
            this.getFyList()
        },
        components: {
            FyDetails,
            FyEdit
        }
    }
</script>

<style scoped>
    .list {
        width: 100%;
    }

    .case-list-footer {
        width: 100%;
        height: 10%;
        position: relative;
    }

    .case-list-footer .pagination {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateY(-50%) translateX(-50%);
    }

    .table {
        width: 100%;
    }
</style>